package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.CustomEntity;
import com.sondon.mayi.entity.LoginEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.NetWorkUtil;
import com.sondon.mayi.util.StringUtil;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : LoginActivity
 * @Author : 蔡文锋
 * @DateTime：2015年5月30日 下午9:24:52
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : {注册/登录页面}
 *
 */
@EActivity(R.layout.login)
public class LoginActivity extends BaseActivity {
	@ViewById
	ImageView login_back;//返回
	@ViewById
	EditText username;//用户名
	@ViewById
	EditText password;//密码
	@ViewById
	Button login_butt;//登陆
	@ViewById
	TextView phone_register;//手机注册
	@ViewById
	TextView forget_passwd;//忘记密码
	
	@AfterViews
	public void initView(){
		//获取客服电话
		AsyncHttpUtil.get(URLConfigs.Custom, null, new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int status, Header[] arg1, String json) {
				if(200==status){
					LogUtil.e(TAG, "客服电话  json ："+json);
					CustomEntity entity=GsonUtil.GetFromJson(json, CustomEntity.class);
					if(entity.isStatus()){
						myprefs.custom_phone().put(entity.getPhone());
					}else{
						myprefs.custom_phone().put("");
					}
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
				
			}
		});
	}
	
	@Click({R.id.phone_register,R.id.login_back,R.id.login_butt,R.id.forget_passwd})
	public void OnClick(View v){
		switch (v.getId()) {
		case R.id.login_butt:
			String name=username.getText().toString().trim();
			String passwd=password.getText().toString().trim();
			
			//判断是否为空
			if(StringUtil.isEmpty(name)||StringUtil.isEmpty(passwd)){
				ToastUtils.show(context, R.string.input_name_passwd);
				return;
			}
			
			//判断是否有网络
			if(!NetWorkUtil.isNetworkAvailable(context)){
				ToastUtils.show(context, R.string.network_unavailable);
				return;
			}
			
			//发送登录请求
			RequestParams params=new RequestParams();
			params.put("username",name);
            params.put("password", passwd);
            params.put("token", myprefs.device_token().get());
            params.put("channel_id", "xiaomi");
			params.put("type", "1");
			LogUtil.e(TAG, "login token :"+myprefs.device_token().get());
			AsyncHttpUtil.post(URLConfigs.Login, params, loginResponseHandler);
			break;
		case R.id.forget_passwd://忘记密码
			openActivity(ForgetPasswdActvitity_.class);
			break;
		case R.id.phone_register://注册新用户
			openActivity(PhoneRegisterActivity_.class);
			break;
		case R.id.login_back://返回
			onBackPressed();
			break;
		default:
			break;
		}
	}
	
	//请求响应监听
	TextHttpResponseHandler loginResponseHandler =new TextHttpResponseHandler() {
		
		@Override
		public void onSuccess(int statusCode, Header[] arg1, String json) {
			if(statusCode==200){
				LogUtil.e(TAG, "LoginActivity  json :"+json);
				LoginEntity loginEntity=GsonUtil.GetFromJson(json, LoginEntity.class);
				if(loginEntity.isStatus()){
					//记录is_login
					myprefs.is_login().put(true);
					//记录Ukey
					myprefs.Ukey().put(loginEntity.getUkey());
					//跳转
//					Bundle bundle=new Bundle();
//					bundle.putInt("position", 0);
//					openActivity(MainTabActivity_.class,bundle);
					myprefs.position().put(0);
					openActivity(MainTabActivity_.class);
					
					LoginActivity.this.finish();
				}else{
					ToastUtils.show(context,loginEntity.getMsg());
				}
			}
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
			ToastUtils.show(context, R.string.network_unavailable);
		}
	};
}
